﻿using System.Linq;
using www.Models;

namespace www.Data.DAO
{
    public class PhotoValueDAO
    {
        #region value
        private RelaxDataContext _context = new RelaxDataContext();
        PhotoRefDAO _photoRefDao = new PhotoRefDAO();
        #endregion

        #region Hàm lấy dữ liệu

        #endregion
        #region Add
        public void Add(PHOTO_VALUE photoValue)
        {
            _context.PHOTO_VALUEs.InsertOnSubmit(photoValue);
            _context.SubmitChanges();

        }
        #endregion
        #region Update

        public bool Update(PHOTO_VALUE photoValue)
        {
            var first = _context.PHOTO_VALUEs.FirstOrDefault(p => p.languagecode == photoValue.languagecode && p.photoid == photoValue.photoid);
            if (first != null)
            {
                first.name = photoValue.name;
                first.tag = photoValue.tag;
                first.image = photoValue.image;
                first.detail = photoValue.detail;
                first.report = photoValue.report;
                _context.SubmitChanges();
                return true;
            }
            return false;
        }
        #endregion

        #region Delete
        public void Delete(long iId, long iLanguageCode)
        {
            var lData = _context.PHOTO_VALUEs.Where(p => p.photoid == iId);
            var delete = lData.Count() == 1 || iLanguageCode == 0;
            if (iLanguageCode != 0)
                lData = lData.Where(p => p.languagecode == iLanguageCode);
            foreach (var item in lData)
            {
                _context.PHOTO_VALUEs.DeleteOnSubmit(item);
            }
            _context.SubmitChanges();
            if (delete)
                _photoRefDao.Delete(iId);
        }
        #endregion
    }
}